package com.syedu.day15;

import java.util.concurrent.*;

public class 线程池运行原理 {
	public  static void main(String[] args) {
		// 核心线程数,最大线程数,保持存活的时间,时间单位,任务队列,饱和策略
		ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 10, TimeUnit.SECONDS,
				new ArrayBlockingQueue<>(2), new ThreadPoolExecutor.AbortPolicy());
		executor.execute(new MyRunnable("第" + 1 + "号线程"));
		executor.execute(new MyRunnable("第" + 2 + "号线程"));
		executor.execute(new MyRunnable("第" + 3 + "号线程"));
		executor.execute(new MyRunnable("第" + 4 + "号线程"));
		executor.execute(new MyRunnable("第" + 5 + "号线程"));
		executor.execute(new MyRunnable("第" + 6 + "号线程"));
		executor.execute(new MyRunnable("第" + 6 + "号线程"));

	}

	static class MyRunnable implements Runnable {
		private String name;

		public MyRunnable(String name) {
			this.name = name;
		}

		@Override
		public void run() {
			System.out.println(name);
			boolean flag =true;
			while (flag) {
				// 让线程一直执行...
				try {
					Thread.sleep(10);
					flag =false;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

        @Override
        public String toString() {
            return name;
        }
    }
}
